package com.solar.etl.transform;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.dom4j.Element;

import com.solar.etl.config.mapping.Field;
import com.solar.etl.spi.Itransform;

public class Javascript implements Itransform {

	@Override
	public String transform(Field field, Element item) {
		String script=item.getTextTrim();
		ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
		try {
			engine.eval("var value ="+field.destValue+";" +
					"var reValue ='';" +
					""+script);
			String reValue = (String)engine.get("reValue");
			field.destValue=reValue;
//			System.out.println(field.destValue+"_"+reValue);
		} catch (ScriptException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String args[]){
		ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
		try {
			engine.eval("var i-=1+1");
		} catch (ScriptException e) {
			e.printStackTrace();
		}
	}
}
